

Skt.ui.handleFollowingChange = function() {
	console.log('Skt.teacherMonitor.isFollowingEnabled()');
	console.log(Skt.teacherMonitor.isFollowingEnabled());
	if ( Skt.teacherMonitor.isFollowingEnabled() ) {
		var room = Skt.model.findRoom(Skt.teacherMonitor.getTeacherRoomId());
		console.log('room');
		console.log(room);
		if ( room != null && (room.getType() == 'screen' || room.getType() == 'question') ) {
			var currentRoom = Skt.sketchpad.getCurrentCanvas() != null ? Skt.sketchpad.getCurrentCanvas().getRoom() : null;
			console.log("Skt.user.type == 'tv' || (currentRoom == null || (currentRoom.getType() == 'screen' || currentRoom.getType() == 'question'))");
			console.log(Skt.user.type == 'tv' || (currentRoom == null || (currentRoom.getType() == 'screen' || currentRoom.getType() == 'question')));
			if ( Skt.user.type == 'tv' || (currentRoom == null || (currentRoom.getType() == 'screen' || currentRoom.getType() == 'question')) )
				Skt.sketchpad.setCurrentCanvas(room.getId(), true);
		}		
	}
};

/* trigger is set only for non-teacher users */
Skt.handlers.handleFollowingEnabledChanged = function(params) {
//	console.log('Skt.handlers.handleFollowingEnabledChanged = function(params) {');
	Skt.ui.handleFollowingChange();
//	console.log('Skt.handlers.handleFollowingEnabledChanged');
//	console.log(params);
};

/* trigger is set only for non-teacher users */
Skt.handlers.handleTeacherRoomChanged = function(params) {
//	console.log('Skt.handlers.handleTeacherRoomChanged = function(params) {');
	Skt.ui.handleFollowingChange();
};



Skt.ui.refreshUserListHandle = function() {
	if ( Skt.user_list.requestersEmpty() )
		$('#user-list-handle').css('background-image', "url('" + Skt.basePath + "/resources/images/tab-users.png')");
	else
		$('#user-list-handle').css('background-image', "url('" + Skt.basePath + "/resources/images/tab-users-attention.png')");
};

Skt.handlers.requestIssuedHandler = function(params) {
	$('#user-status-' + params.userId).addClass('user-status-attention');
	$('#user-status-' + params.userId).html('!');
	Skt.ui.refreshUserListHandle();
};

Skt.handlers.requestDismissedHandler = function(params) {
	$('#user-status-' + params.userId).removeClass('user-status-attention');
	$('#user-status-' + params.userId).html('&#149;');
	Skt.ui.refreshUserListHandle();
};



Skt.handlers.currentCanvasChangeHandler = function(params) {
	
	$('#requestFocusBtn').hide();
	var canvas = params.canvas;
	
	if ( canvas != null ) {
		var room = canvas.getRoom();
		var roomList = room.getRoomList();
		
		if ( roomList != null ) {
			var group = roomList._group;
			if ( group != null && group.isMember(Skt.user.id) && group.hasTVMember() )
				$('#requestFocusBtn').show();
		}
	}
	
};


Skt.handlers.tvGroupJoinHandler = function(params) {
	
	if ( Skt.user.id == params.tvId ) {
		$('#forwardForm input[name=group_name]').val(params.groupName);
		$('#forwardForm input[name=group_type]').val(params.groupType);
		$('#forwardForm input[name=activity_id]').val(params.activityId);
		$('#forwardForm input[name=subject_version_id]').val(params.subjectVersionId);
//		$('#forwardForm input[name=user_id]').val(Skt.user.id);
		if ( params.subjectVersionId != Skt.subjectVersion.id || params.activityId != Skt.activity.id ) {
			$('#forwardForm').submit();
		} else {
			$('#forwardForm input[name=group_name]').val(params.groupName);
			$('#forwardForm input[name=group_type]').val(params.groupType);
			var group = Skt.groupsManager.getGroup($('#forwardForm input[name=group_name]').val(), $('#forwardForm input[name=group_type]').val());
//			Skt.groupsManager.addGroup(group.getName(), group.getGroupType(), group.getOwnerId());
			group.addMember(Skt.user.id);
			$('#start_message').html(Skt.user.name + ' ' + Skt.user.lastname + '<br/>' + group.getName());
		}
	}
	
};

Skt.handlers.tvRoomFocusHandler = function(params) {
	var group = Skt.groupsManager.getGroup(params.groupName, params.groupType); 
	if ( group != null && group.isMember(Skt.user.id) )
		Skt.sketchpad.setCurrentCanvas(params.roomId);
};

Skt.handlers.followingJoinHandler = function(params) {
	$('#forwardForm input[name=activity_id]').val(params.activityId);
	$('#forwardForm input[name=subject_version_id]').val(params.subjectVersionId);
	if ( params.subjectVersionId != Skt.subjectVersion.id || params.activityId != Skt.activity.id )
		$('#forwardForm').submit();
};


Skt.handlers.addUserCallback = function(attr_list) {
//	var userId = attr_list.userId;
//	$('#students_ul').children("li[data-id="+userId+"]").find(".user-status").addClass("user-status-online");
};

Skt.handlers.removeUserCallback = function(attr_list) {
//	var userId = attr_list.userId;
//	$('#students_ul').children("li[data-id="+userId+"]").find(".user-status").removeClass("user-status-online");
};

Skt.handlers.changeInstancesCallback = function(params) {
	var userId = params.userId;
//	console.log("Skt.user_list.userIsOnline(userId)");
//	console.log(Skt.user_list.userIsOnline(userId));
	if ( Skt.user_list.userIsOnline(userId) )
		$('#students_ul').children("li[data-id="+userId+"]").find(".user-status").addClass("user-status-online");
	else
		$('#students_ul').children("li[data-id="+userId+"]").find(".user-status").removeClass("user-status-online");
};

Skt.handlers.userListHandler = function (params) {
//	console.log("Skt.handlers.userListHandler = function (params) {");
	switch (params.action_name) {
		case "addUser": Skt.handlers.addUserCallback(params.attr_list); break;
		case "removeUser": Skt.handlers.removeUserCallback(params.attr_list); break;
		case "changeInstances": Skt.handlers.changeInstancesCallback(params.attr_list); break;
		default: break;
	}
};

Skt.handlers.invitationHandler = function(params) {
	if ( params.action == "issue" ) {
		Skt.invitedGroup = {
			name: params.name,
			groupType: params.groupType,
			ownerId: params.ownerId
		};
		
		$('#group-invite-alert').hide();
		$('#alert-message').html( Skt.user_list.getUser(params.inviterId).name + ' has invited you to the group "' + params.name + '" made by ' + Skt.user_list.getUser(params.ownerId).name );	
		$('#group-invite-alert').show('slow');
	} else if ( params.action == "dismiss" ) {
		$('#group-invite-alert').hide('fast');
	}
};

Skt.ui.buildMembersHtml = function(group) {
	var html = '';
	
	var members = group.getMembers();
	for ( var n = 0; n < members.length; ++n ) {
		var user = Skt.user_list.getUser("" + members[n]);
//		var span = '';
//		if ( group.isOwner(Skt.user.id) &&  )
//			span = '<span data-memberid="' + user.id +'" onclick="console.log(this);">X</span>';
		html = html + '<li data-userid="' + user.id + '">' + user.name + '</li>';
	}
	
	return html;
};

Skt.ui.buildGroupRoomsHtml = function (group) {
	var onclick = 'onclick="$(this).parent().next().slideToggle(); $(this).parent().children(\'.triangle\').toggleClass(\'ui-icon-triangle-1-e ui-icon-triangle-1-s\');"';
	return '<div class="group-container">' +
		'<div class="groupal-room-list-name" style="background-color: rgba(255,255,255,0.5); margin: 4px 0px; cursor: pointer;">' +
			'<span class="ui-icon ui-icon-triangle-1-s triangle" ' + onclick + '></span>' +
			'<span class="ui-icon group-type-icon ' + (group.getGroupType() == 'work' ? 'ui-icon-star' : 'ui-icon-note' ) + '" ' + onclick + '></span>' +
			'<span class="group-name" ' + onclick + '>' + group.getName() + '</span>' +
			'<i class="icon-plus btn btn-mini" style="float: right; position: relative; right: 5px; height: 12px; width: 10px; line-height: inherit;" onclick="Skt.model.addRoom(\'groupal\',{type:\'' + group.getGroupType() + '\', name:\'' + group.getName() + '\'});"></i>' +
			'<i class="icon-picture btn btn-mini import-slides-btn" style="float: right; position: relative; right: 5px; height: 12px; width: 10px; line-height: inherit;" data-type="groupal" data-toggle="modal" data-target="#import-slides-modal"></i>' +
		'</div>' +
		'<ul id="rooms-groupal-' + group.getId() + '" class="rooms-ul nav nav-pills" data-side="right"></ul>' +
	'</div>';
};

Skt.ui.registerGroup = function(group) {
	if ( group.isMember(Skt.user.id) && $.inArray(group.getId(), Skt.watchedGroups) < 0 ) {
		$('#groupal-rooms-container').append(Skt.ui.buildGroupRoomsHtml(group));
		
		var rooms = group.getRoomList().getRooms();
		for ( var k = 0; k < rooms.length; ++k ) {
			Skt.sketchpad.addCanvasFromRoom({
				roomId: rooms[k].room.getId(),
				room: rooms[k].room,
				type: rooms[k].room.getType(),
				options: {}
			});
		}
		
//		Skt.ui.set_draggable_room_list('rooms-groupal-' + group.getId());
					
		Skt.watchedGroups.push(group.getId());
	}
};

Skt.handlers.memberEventHandler = function(params) {
//	console.log("Skt.handlers.memberEventHandler = function(params) {");
	var name = params.groupName;
	var groupType = params.groupType;
	var group = Skt.groupsManager.getGroup(name, groupType);
	
	if ( params.action == 'addMember' && groupType == 'work' ) {
		var userId = params.userId;
		var user = Skt.user_list.getUser(userId);
		user.workGroupName = name;
	}	
	setTimeout(function() {
		$('#group-list-' + group.getId()).html(Skt.ui.buildMembersHtml(group));
		Skt.ui.registerGroup(group);
		Skt.handlers.currentCanvasChangeHandler({canvas: Skt.sketchpad.getCurrentCanvas()});
	}, 1000);
	
};

Skt.handlers.groupEventHandler = function(params) {
	var action = params.action;
	var group = params.group;
	
	if ( action == "addGroup" ) {
		
		var html = '<h2 data-name="' + group.getName() + '" data-grouptype="' + group.getGroupType() + '" class="group-h2"><span class="ui-icon ui-icon-triangle-1-e"></span><a href="#">' + group.getName() + '</a></h2>';
		
		html = html + '<div><ul id="group-list-' + group.getId() + '">' + Skt.ui.buildMembersHtml(group) + '</ul></div>';
		
		var elem = $(html); 
		
		elem.click( function() {
			$(this).find("> .ui-icon")
				.toggleClass( "ui-icon-triangle-1-e ui-icon-triangle-1-s")
				.end()
				.next()
				.slideToggle();
			return false;
		}).next().hide();
		
		elem.droppable({
			hoverClass : "dropableGroup",
			accept : "#rooms_question .room, .student-li",
			drop : function(event, ui) {
				var group = Skt.groupsManager.getGroup(this.getAttribute('data-name'), this.getAttribute('data-grouptype'));
				var user = Skt.user_list.getUser(ui.draggable.data('id'));
				
				if ( group != null ) {
					if ( !user ) {
						alert("Offline users can't be member of groups");
						return false;
					} else if ( user.rol == "tv" ) {
						if ( Skt.user.type == "teacher" && group.getGroupType() == "work" ) {
							Skt.tvManager.requestGroupJoin(user.id, Skt.subjectVersion.id, Skt.activity.id, group.getName(), group.getGroupType());
						} else {
							alert("TVs can only be assigned to work groups by the teacher");
						}
					} else if( group.getGroupType() == "note" ) {
						group.invite(Skt.user.id, ui.draggable.data('id'));
					} else if ( group.getGroupType() == "work" && Skt.user.type == "teacher" ) {
						if ( user.workGroupName )
							alert("This user is already on a work group");
						else
							group.addMember(ui.draggable.data('id'));
					} else { 
						alert("You don't have permission to modify work groups members");
					}
				}
			}
		});
		
		if ( group.getGroupType() == "note" )
			$('#notes-groups-accordion').append(elem);
		else
			$('#work-groups-accordion').append(elem);
		
		Skt.ui.registerGroup(group);

	}
	
};